/*
 * Here is the controller for processing the requests.
 *
 *
 */
// create the namespace
jmaki.namespace("jmaki.data.catalog");

// create a catalog object for the products so we do not reload them
jmaki.data.catalog = {};


jmaki.subscribe("/controller*", function(args) {
    // get the id of the product we want to view
    var productId = args.message.value;
    jmaki.log("Controller : setting items to " + productId);
    if (!jmaki.data.catalog[productId]) {
      jmaki.doAjax( {
            url : 'data-' + productId + '.json',
            callback : function(req) {
                // clear the items in the browser
                jmaki.publish('/ibrowser/clear', {});                
                // convert the response into a JSON object
                var _products = eval("(" + req.responseText + ")");
                jmaki.data.catalog[productId] = _products;
                jmaki.publish('/ibrowser/setItems', { id : productId, items : _products.items});
            }
          });
    } else {
        // clear the items in the browser
        // load from the cached data
        var _products = jmaki.data.catalog[productId];
        jmaki.publish('/ibrowser/setItems', { id : productId, items : _products.items});
    }
    
});

jmaki.subscribe("/jmaki/ibrowser/onSelect", function(args) {
    
    // find out the price id based on the widget id
    var pNode = document.getElementById(args.widgetId + "_price");
    jmaki.log("widgetId=" + args.targetId + " pnode==" + pNode);
    var item = args.value;
    pNode.innerHTML = item.price;
    var rNode = document.getElementById(args.widgetId + "_rating");
    rNode.innerHTML = item.price + "*****";
    
});
/**
 * Assumes the item has been loaded (can't add to cart if not visible)
*/

jmaki.subscribe("/jmaki/store/addToCart", function(args) {
    // get the id of the product we want to view
    var itemId = args.itemId;

    var category = jmaki.data.catalog[args.category];
    jmaki.log("checking category " + jmaki.inspect(category));    
    for (var i=0; i < category.items.length;i++) {
       if (category.items[i].id == itemId) {
          // jmaki.log("we have the item=" + jmaki.inspect));
           jmaki.publish('/jmaki/cart/addItem', {value : category.items[i]} );
           break;
       }
    }
    
});

/*
 * This code get called after jMaki is initialized to populate the list
 * 
 */
function loadCatalogData(args) {   
    // start with birds
    var productId = 'birds';
    jmaki.log("Initialize : setting items to " + productId);
    jmaki.doAjax( {
            url : 'data-' + productId + '.json',
            callback : function(req) {
                // clear the items in the browser
             //   jmaki.publish('/ibrowse/clear', {});                
                // convert the response into a JSON object
                var _products = eval("(" + req.responseText + ")");
                jmaki.data.catalog[productId] = _products;
                jmaki.publish('/ibrowser/setItems', { id : productId, items : _products.items});
            }
          });  
    
} 

// wait for the widget to load
jmaki.subscribe("/jmaki/runtime/widget/loaded", function(args){
// TODO : Need to fix this logic    
    if (args.id.indexOf("jmaki") != -1) {
        loadCatalogData();
    }    
});



/*
 * This code get called after jMaki is initialized to populate the list
 * 
 */

jmaki.subscribe("/jmaki/cart/checkout", function(args) {
	jmaki.debug = true;
    jmaki.log("Check out has happened!");
    // do checkout here
});